package TestArray;

import java.util.Scanner;

public class ArrarDemo1 {
    public static void main(String[] args) {
        //数组的三种初始化方法
        //静态初始化
        int[] a1={2,4,6,8};
        //默认初始化
        int[] b=new int[3]; //默认
        //动态初始化
        int[] c=new int[2];
        c[0]=1; //给数组元素赋值
        c[1]=2; //给数组元素赋值


        int[] arr=new int[5];
        //从键盘接收数据,为数组元素赋值
        System.out.println("请输入5个元素:");
        Scanner scanner=new Scanner(System.in);
        for (int i=0;i<arr.length;i++){
            int a=scanner.nextInt();
            arr[i]=a;
        }
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        //求数组元素的累加和
        int sum=0;
        for (int i=0;i<arr.length;i++){
            sum=sum+arr[i];
        }
        System.out.print(sum);
    }
}
